import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/core/utils/user_manager.dart';
import 'package:enterprise_v6_app/app/data/entities/application/special_list/job_plan_details_entity.dart';

class JobPlanRequestController extends GetxController with GetSingleTickerProviderStateMixin {
  /// 是否申请
  bool isApply = (Get.arguments == null);

  /// 当前登录人是否为辨识人
  bool isIdentityUser = false;

  /// 当前登录人是否为创建人
  bool isCreateUser = false;

  /// 作业计划id
  String? id;

  /// 是否可以编辑
  bool isEnabled = false;

  /// 计划申请详情实体
  JobPlanDetailsEntity jobPlanDetailsEntity = JobPlanDetailsEntity();

  late TabController tabController;

  @override
  void onInit() {
    super.onInit();
    tabController = TabController(length: 2, vsync: this);
    // 如果为修改，则获取详情
    if (!isApply) {
      id = Get.arguments as String;
      getPlanDetail();
    }else{
      isEnabled = true;
      update();
    }
  }

  // 获取计划申请详情
  void getPlanDetail() async {
    final res = await HttpUtil().generateGetRequest<JobPlanDetailsEntity>(
      Interface.getTicketPlanDetail,
      queryParameters: {'id': id},
    ).execute(isShowLoading: true);

    res.onSuccess((result) async {
      if (result.data != null) {
        jobPlanDetailsEntity = result.data!;
        // 是否为当前登录人
        isIdentityUser = jobPlanDetailsEntity.identityUserId == UserManager.getUserId();
        // 是否为当前创建人
        isCreateUser = jobPlanDetailsEntity.createById == UserManager.getUserId();
        // 如果为申请，或登录人为创建人，则可以进行修改
        if (isCreateUser) {
          isEnabled = true;
        }
        // 如果为修改，且为风险辨识人，则默认选中第二个tab
        if (isIdentityUser) {
          tabController.animateTo(1);
        }
        update();
      }
    });
  }

  @override
  void onClose() {
    super.onClose();
    tabController.dispose();
  }
}
